package com.xszx.controller;


import com.xszx.entity.*;
import com.xszx.service.AdminService;
import com.xszx.service.SensitiveFilterService;
import com.xszx.service.UserService;
import org.apache.commons.io.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.util.*;

/**
 * (User)表控制层
 *
 * @author makejava
 * @since 2024-10-31 09:52:03
 */

@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;
    @Autowired
    private AdminService adminService;

    //申请成为商家
    @RequestMapping("/forBusiness")
    @CrossOrigin(origins ="http://localhost:5173")
    @ResponseBody
    public Integer forBusiness(User user) {
        //判断是否卖够500
        Integer t = userService.getisSalesOver500(user);
        if (t != null) {
            Integer i = userService.forBusiness(user);
            if (i != null) {
                return 1;//申请成功
            }
        }
        return 0;//销售金额不够
    }
    //上架商品
    @RequestMapping("/addGoods")
    @CrossOrigin(origins ="http://localhost:5173")
    @ResponseBody
    public Integer addGoods(Goods goods) throws Exception {
        //添加商品
          userService.addGoods(goods);
        //商品审核
            Integer i= adminService.checkGood(goods.getGid());
            if(i!=null){
                return 1;//申请成功
            }
            return 0;//申请失败
    }

    //查看买家对自己商品的评价
    @RequestMapping("/getComment")
    @CrossOrigin(origins ="http://localhost:5173")
    @ResponseBody
    public Object getComment(String gid){
        List<Comments> list= adminService.getComment(Integer.valueOf(gid));
        return  list;
    }

    //用户去评价（敏感词屏蔽）：对购买的商品进行评价，系统自动屏蔽敏感词。
    @RequestMapping("/goComment")
    @CrossOrigin(origins ="http://localhost:5173")
    @ResponseBody
    public Object goComment(@RequestBody Comments comment){

        SensitiveFilterService filter = SensitiveFilterService.getInstance();
        String txt = comment.getComment();
    //如果需要过滤则用“”替换
    //如果需要屏蔽，则用“*”替换
        String hou = filter.replaceSensitiveWord(txt, 1, "*");
        System.out.println("替换前的文字为：" + txt);
        System.out.println("替换后的文字为：" + hou);
        comment.setComment(hou);
        Integer i= adminService.goComment(comment);
        if(i!=null){
            return 1;//评价提交成功
        }
        return 0;//评价提交失败

    }
    //删除订单 （退款中不能删）
    @RequestMapping("/delOrder")
    @CrossOrigin(origins ="http://localhost:5173")
    @ResponseBody
  public Object delOrder(Order order){
         //判断订单是否在退款中
       Integer i= adminService.checkIfrtmoney(order);
        if(i==null){
            adminService.delOrder(order);
            return 1;//订单删除成功
        }
        return 0;//订单正在退款中
  }
   //查看商品详情
    @RequestMapping("getGoodsDetails")
    @CrossOrigin(origins ="http://localhost:5173")
    @ResponseBody
    public Object getGoodsDetails(String gid){
       Goods goods= adminService.getGoodsDetails(Integer.valueOf(gid));
       return goods;
    }

    //加入购物车
    @RequestMapping("/addShopping")
    @CrossOrigin(origins ="http://localhost:5173")
    @ResponseBody
    public Object addShopping(@RequestParam("uid") int uid, Goods goods){
        System.out.println(uid);
        Integer i=adminService.addShopping(uid,goods.getGid());
       if(i!=null){
           return 1;//加入购物车成功
       }
       return 0;
    }

//    从购物车中删除
    @RequestMapping("/delShopping")
    @CrossOrigin(origins ="http://localhost:5173")
    @ResponseBody
    public Object delShopping(Shopping shopping){
        userService.delShopping(shopping);
        return 1;
    }

    //查看某一个商品的全部评价
    @RequestMapping("/getGoodsComments")
    @CrossOrigin(origins ="http://localhost:5173")
    @ResponseBody
    public Object getGoodsComments(Goods goods){
        List<Comments>  list= userService.getGoodsComments(goods);
        System.out.println(list);
        return list;
    }


    //    个人信息管理业务
//    1.获取用户信息
//    2.回显用户信息
//    3.修改
    @RequestMapping("/upUser")
    @CrossOrigin(origins = "http://localhost:5173")
    @ResponseBody
    public Object upUser(@RequestParam("uid") String uid){
        User user=adminService.upUser(Integer.parseInt(uid));
        return user;
    }
    @RequestMapping("/updateUser")
    @CrossOrigin(origins = "http://localhost:5173")
    @ResponseBody
    public Object updateUser(User user,@RequestParam("county") String se3)throws IOException{
        System.out.println("用户名"+user);
        System.out.println("县"+se3);
        String address=adminService.getAddress(se3);

        user.setUaddress(address);
//        修改用户信息
        userService.updateUser(user);
        return 1;
    }
    //    图片上传
    @RequestMapping("/image")
    @CrossOrigin(origins = "http://localhost:5173")
    @ResponseBody
    public Object test10(@RequestParam("file") MultipartFile image,
                         HttpServletRequest req, HttpServletResponse resp) throws IOException {
        //获取文件路径
        String path ="D:\\java\\letgo-front-end\\src\\assets\\user";

        if(!image.isEmpty()){
            FileUtils.copyInputStreamToFile(image.getInputStream(), new File(path, Objects.requireNonNull(image.getOriginalFilename())));
        }
        Map<String, String> response = new HashMap<>();
        response.put("filename", image.getOriginalFilename());

        // 返回 JSON 字符串
        return ResponseEntity.ok(response);
    }

    //    回显
    @RequestMapping("/upGoods")
    @CrossOrigin(origins = "http://localhost:5173")
    @ResponseBody
    public Object upGoods(Goodtype goodtype){
        return goodtype;
    }

    //    修改商品信息
//    在订单表中查询是否存在该商品
//    在购物车中查询是否存在该商品
    @RequestMapping("/check")
    @CrossOrigin(origins = "http://localhost:5173")
    @ResponseBody
    public Object check(Goods goods){
        Orders orders=userService.checkOrder(goods);
        Shopping shopping=userService.checkShopping(goods);
        if(orders!=null||shopping!=null){
            Goods goods1=userService.getGoods(goods);
            if(!goods1.getGname().equals(goods.getGname())||goods1.getGtid()!=goods.getGtid()){
                //            if修改过
//            将商品表中该商品状态改为下架
                userService.delGoods(goods);
//            再次在商品表中添加商品
                userService.addGoods(goods);
            }
        }else {
            //        直接修改
            userService.updateGoods(goods);
        }
        return 1;

    }

    //    下架商品
    @RequestMapping("/delGoods")
    @CrossOrigin(origins = "http://localhost:5173")
    @ResponseBody
    public Object delGoods(Goods goods){
        userService.delGoods(goods);
        return 1;
    }

    //同城查询
    @RequestMapping("/getSameAddress")
    @CrossOrigin(origins = "http://localhost:5173")
    @ResponseBody
    public Object getSameAddress(User user){
        List<Goods> goods=userService.getSameAddress(user);
        System.out.println(goods);
        return goods;
    }

//    支付操作
    @RequestMapping("userPay")
    @CrossOrigin(origins = "http://localhost:5173")
    @ResponseBody
    public Object userPay(Orders orders,int uid){
        userService.userPay(orders);
        Shopping shopping=new Shopping();
        shopping.setGid(orders.getGid());
        shopping.setUid(uid);
        delShopping(shopping);
        return 1;
    }

//    个人收货地址的管理
//    查看
    @RequestMapping("/getUserAddress")
    @CrossOrigin(origins = "http://localhost:5173")
    @ResponseBody
    public Object getUserAddress(User user){
        List<UserAddress> userAddressList=userService.getUserAddress(user);
        return userAddressList;
    }
//    个人收货地址增加
    @RequestMapping("/addUserAddress")
    @CrossOrigin(origins = "http://localhost:5173")
    @ResponseBody
    public Object addUserAddress(@RequestBody UserAddress userAddress){

        System.out.println("添加收货地址"+userAddress);
        userService.addUserAddress(userAddress);
        return 1;
    }
//    个人收货地址删除
    @RequestMapping("/delUserAddress")
    @CrossOrigin(origins = "http://localhost:5173")
    @ResponseBody
    public Object delUserAddress(@RequestBody UserAddress userAddress){
        userService.delUserAddress(userAddress);
        return 1;
    }


//    个人收货地址修改回显
    @RequestMapping("upUserAddress")
    @CrossOrigin(origins = "http://localhost:5173")
    @ResponseBody
    public Object upUserAddress (@RequestBody UserAddress userAddress){
       UserAddress userAddress1=  userService.upUserAddress(userAddress);
        System.out.println("个人信息回显"+userAddress1);
        return userAddress1;
    }

//  个人收货地址修改
    @RequestMapping("/updateUserAddress")
    @CrossOrigin(origins = "http://localhost:5173")
    @ResponseBody
    public Object updateUserAddress(@RequestBody UserAddress userAddress){
        userService.updateUserAddress(userAddress);
        return 1;
    }

//   折线图
    @RequestMapping("/LineChart")
    @CrossOrigin(origins = "*")
    @ResponseBody
    public Object LineChart(){
        double[] array =userService.LineChart();
      List<Double> list=  new ArrayList<>();
        for(int i=0;i<array.length;i++){
             list.add(array[i]);
        }
        return list;
    }

//    我发布的商品
    @RequestMapping("/posted")
    @CrossOrigin(origins = "http://localhost:5173")
    @ResponseBody
    public Object posted(User user){
        List<Goods> goodsList=userService.posted(user);
        return goodsList;
    }
//我卖出的商品
    @RequestMapping("/sold")
    @CrossOrigin(origins = "http://localhost:5173")
    @ResponseBody
    public Object sold(User user){
        List<Orders> ordersList=userService.sold(user);
        return ordersList;
    }

//    我买到的商品
    @RequestMapping("/bought")
    @CrossOrigin(origins = "http://localhost:5173")
    @ResponseBody
    public Object bought(User user){
        List<Orders> ordersList=userService.bought(user);
        return ordersList;
    }

//    修改密码
    @RequestMapping("/changePwd")
    @CrossOrigin(origins = "http://localhost:5173")
    @ResponseBody
    public Object changePwd(@RequestParam("password") String pwd,@RequestParam("uid") int uid){
        System.out.println(pwd);
        System.out.println(uid);
        userService.changePwd(pwd,uid);
        return 1;
    }

//    查看自己的评价
    @RequestMapping("/checkReviews")
    @CrossOrigin(origins = "http://localhost:5173")
    @ResponseBody
    public Object checkReviews(User user){
        List<Comments> commentsList=userService.checkReviews(user);
        return commentsList;
    }

//    获取商品列表
    @RequestMapping("/getSYGoodsList")
    @CrossOrigin(origins = "http://localhost:5173")
    @ResponseBody
    public Object getSYGoodsList(){
         List<Goods> goodsList=userService.getSYGoodsList();
        return goodsList;
    }

    @RequestMapping("/getSYMHGoodsList")
    @CrossOrigin(origins = "http://localhost:5173")
    @ResponseBody
    public Object getSYMHGoodsList(int gtid){
        List<Goods> goodsList=userService.getSYMHGoodsList(gtid);
        return goodsList;
    }

    }






